﻿package mvc_controller
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import mvc_model.RegisterCommand;
	import mvc_model.NerdsPageModel;
	import mvc_view.View;
	import mvc_view.RegisterView;
	import mvc_model.UploadFile;
	import mvc_model.Model;

	public class RegisterController
	{
		private var _view:RegisterView;
		private var _uploadFile:UploadFile;

		public function RegisterController(model:UploadFile, view:RegisterView)
		{
			_view = view;
			_uploadFile = model;
		}

		public function submitAvatarButton(button:DisplayObject)
		{
			button.addEventListener(MouseEvent.CLICK, openBrowserBox);
			button.addEventListener(MouseEvent.ROLL_OVER,doRollOver);
			button.addEventListener(MouseEvent.ROLL_OUT,doRollOut);

			MovieClip(button).buttonMode = true;
			MovieClip(button).useHandCursor = true;
		}

		public function openBrowserBox(event:Event = null)
		{
			_uploadFile.browseHandler(null);
		}

		public function submitRegisterButton(button:DisplayObject)
		{
			button.addEventListener(MouseEvent.CLICK, submitRegistration);

			button.addEventListener(MouseEvent.ROLL_OVER, doRollOver);
			button.addEventListener(MouseEvent.ROLL_OUT, doRollOut);

			MovieClip(button).buttonMode = true;
			MovieClip(button).useHandCursor = true;
		}

		public function submitRegistration(event:Event = null)
		{
			if (_uploadFile == null) return;
			_uploadFile.uploadHandler(null);

			var registerCommand:RegisterCommand = new RegisterCommand(
				_view.username,
				_view.password,
			  	_view.firstName, 
				_view.lastName,
			 	_view.email, 
				_view.dateOfBirth,
			  	_view.city, 
				_view.country, 
				_view.gender, 
			  	_view.availability, 
				_uploadFile.fileName);
			
			registerCommand.execute();
			
			_view.clearAll();
		}

		function doRollOver(e:MouseEvent):void
		{
			switch (e.currentTarget.name)
			{
				case "browse_files_btn" :
					e.currentTarget.browse_txt.textColor = 0x00FF00;
					break;
				case "submit_btn" :
					e.currentTarget.submit_txt.textColor = 0x00FF00;
					break;
			}
		}

		function doRollOut(e:MouseEvent):void
		{
			switch (e.currentTarget.name)
			{
				case "browse_files_btn" :
					e.currentTarget.browse_txt.textColor = 0x333333;
					break;
				case "submit_btn" :
					e.currentTarget.submit_txt.textColor = 0x333333;
					break;
			}
		}
	}
}